﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
public partial class Admin_adminProductAttribute : System.Web.UI.Page
{
    private int selectIndex = 0;
    AttributeBll aBll = new AttributeBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {
                string action = Request.QueryString["action"].ToString();
                switch (action.Trim())
                {
                    case "add":
                        add();
                        break;
                    case "del":
                        del();
                        break;
                    case "edit":
                        edit();
                        break;
                    case "save":
                        save();
                        break;
                    default:
                        break;
                }
            }
        }
        this.Repeater1.DataSource = aBll.GetModel();
        this.Repeater1.DataBind();
    }
    #region 属性标记下拉框
    public string attributeDp(string id,int count) 
    {
        string str = "<select name=\""+id+"\" id=\"" + id + "\">";
        for (int i = 0; i < count; i++)
        {
            if (i == selectIndex)
            {
                str += "<option selected=\"selected\" id=\"" + id + "\" value=\"" + i + "\">" + i + "</option>";
            }
            else
                str += "<option value=\"" + i + "\">" + i + "</option>";
        }
        return str;    
    }
    #endregion

    public void add() 
    {
        Attribute attri = new Attribute();
        attri.attributeName = Request.Form["AttributeName"].ToString();
        attri.Mark = Convert.ToInt32(Request.Form["attr_add"]);
        if (attri.attributeName != ""&&attri.Mark.ToString()!="")
        {
            if (aBll.Insert(attri))
            {
                Response.Write("1");
                Response.End();
            }
        }
        else 
        {
            Response.Write("属性名/标识不能为空！");
            Response.End();
        }
    }

    public void del() 
    {
        int aid = Convert.ToInt32(Request.QueryString["id"]);
        if (aBll.Delete(aid))
        {
            Response.End();
        }
    }

    public void edit() 
    {
        int aid = Convert.ToInt32(Request.QueryString["id"]);
        Attribute atr = aBll.GetModel(aid);        
        this.attributeid_txt.Value =aid.ToString();
        this.attributename_txt.Value = atr.attributeName;
        selectIndex = atr.Mark;
    }

    public void save() 
    {
        
        int aid = Convert.ToInt32(Request.QueryString["id"]);
        string name = Request.QueryString["name"].ToString();
        Attribute atr = new Attribute();       
        atr.attributeID = aid;
        atr.attributeName = name;
        atr.Mark = Convert.ToInt32(Request.QueryString["mark"]);
        if (aBll.Update(atr))
        {
            Response.Write("修改成功！");
            Response.End();
        }
    }

}
